<?php
    /**
     * Simple friendly URL(Support Chinese)
     * Collect by icyleaf.
     * URI: http://www.icyleaf.cn/2008/07/24/seo-friendly-url-in-php
     * Date: 07/25/2008
     */
    require_once ("class-snoopy.php");

    /**
     * Simple friendly url function
     *
     * Name: friendlyURL
     * Date: Jun 23, 2008
     * Purpose: friendly url for SEO
     * URI: http://htmlblog.net/seo-friendly-url-in-php/
     * @author Asvin Balloo
     * @version 1.0
     * @param string
     * @return string
     */
	function friendlyURL($string)
	{
		$string = preg_replace("`\[.*\]`U","",$string);
		$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$string);
		$string = htmlentities($string, ENT_COMPAT, 'utf-8');
		$string = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $string );
		$string = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $string);
		return strtolower(trim($string, '-'));
	}

    /**
     * Name: Translator Function with Google Translate tools
     * Date: 07/25/2008
     * Purpose: Translate target language to english
     * mod from jiangdong ( http://www.storyday.com/html/y2007/1202_auto-slug-translate-plugin.html )
     * @author: icyleaf ( http://www.icyleaf.cn )
     * @version: 0.8
     * @param string
     * @return string
     */
    function translate($text, $lang = "")
    {
        $snoopy = new Snoopy;
        if($lang=="chinese")
            $args = "zh|en";
        else if($lang=="japanese")
            $args = "ja|en";
        else
            $args = "auto|en";

        $url = "http://google.com/translate_t?langpair={$args}";
        $submit_vars["text"] = $text;
        $submit_vars["ie"] = "UTF8";
        $submit_vars["hl"] = "zh-CN";
        $submit_vars["langpair"] = $args;
        $snoopy->submit($url,$submit_vars);
        $htmlret = $snoopy->results;
        $htmlret = str_replace('"','',$htmlret);
        $htmlret = str_replace("''","",$htmlret);
        $res_array = explode('<div id=result_box dir=ltr>',$htmlret);
        $res_array = explode('</div>',$res_array[1]);
        return $res_array[0];
    }
?>
